package com.clas.liu.myapplication.view.adapter;

import android.app.Activity;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.clas.liu.myapplication.R;
import com.clas.liu.myapplication.view.activity.impl.SearchActivity;

import java.util.List;

/**
 * Created by liu on 2019/5/11.
 */

public class HotSearchAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private SearchActivity activity;
    private List<String> data;

    public HotSearchAdapter(SearchActivity activity, List<String> data) {
        this.activity = activity;
        this.data = data;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        HistoryViewHolder holder =  new HistoryViewHolder( LayoutInflater.from(parent.getContext()).
                inflate(R.layout.item_search_history, parent, false));
        return holder;
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder1, int position) {
        HistoryViewHolder holder = (HistoryViewHolder)holder1;
        final String s = data.get(position);
        holder.tvHistory.setText(s);
        holder.tvHistory.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                activity.startSsearch(s);
            }
        });
    }

    public void updateList(List<String> list) {
        this.data = list;
        this.notifyDataSetChanged();
    }

    @Override
    public int getItemCount() {
        return data.size();
    }

    public class HistoryViewHolder extends RecyclerView.ViewHolder {

        TextView tvHistory;

        public HistoryViewHolder(@NonNull View itemView) {
            super(itemView);
            tvHistory = itemView.findViewById(R.id.item_tv);
        }
    }
}
